package com.heitian.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.heitian.ssm.dao.DemoVideoDao;
import com.heitian.ssm.dao.VideoDao;
import com.heitian.ssm.model.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Zhangxq on 2016/7/15.
 */

@Controller
@RequestMapping("/demovideo")
public class DemoVideoController {

    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private DemoVideoDao demoVideoDao;

    @RequestMapping(value = "/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponsee selectVideo(@PathVariable("page") String page) {

        PageHelper.startPage(Integer.valueOf(page),5);
        List<Video> videoList = demoVideoDao.selectAllVideos();
        return new JsonResponsee(videoList);
    }


    @RequestMapping(method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponsee selectVideo() {

        List<Video> videoList = demoVideoDao.selectVideo();
        return new JsonResponsee(videoList);
    }

    @RequestMapping(value = "/nt0/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponsee selectNt0(@PathVariable("page") String page) {

        List<Focusimg> focusimg = demoVideoDao.selectAllFocusimg();
        PageHelper.startPage(Integer.valueOf(page),5);
        List<News> newslist = demoVideoDao.selectAllNews();
        Nt0 nt0 = new Nt0(focusimg, newslist);
        return new JsonResponsee(nt0);
    }

    @RequestMapping(value = "/nt1/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponsee selectNt1(@PathVariable("page") String page) {

        PageHelper.startPage(Integer.valueOf(page),7);
        List<News> newslist = demoVideoDao.selectAllNewsN1();
        Nt1 nt1 = new Nt1(newslist);
        return new JsonResponsee(nt1);
    }

    @RequestMapping(value = "/nt2/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponsee selectNt2(@PathVariable("page") String page) {

        PageHelper.startPage(Integer.valueOf(page),7);
        List<News> newslist = demoVideoDao.selectAllNewsN2();
        Nt2 nt2 = new Nt2(newslist);
        return new JsonResponsee(nt2);
    }

    @RequestMapping(value = "/pic/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<Picture> selectPic(@PathVariable("page") String page) {

        PageHelper.startPage(Integer.valueOf(page),15);
        List<Picture> piclist = demoVideoDao.selectAllPics();
        return piclist;
    }

    @RequestMapping(value = "/collect", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponse showAssess(@RequestParam Long uid) {

        List<Video> videoList = demoVideoDao.selectCollect(String.valueOf(uid));
        return new JsonResponse(JsonResponse.SUCCESS, videoList);
    }

    @RequestMapping(value = "/addCollect", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponse showAssess(@RequestParam Long uid,@RequestParam Long bid) {

        int isok = demoVideoDao.addCollect(String.valueOf(uid),String.valueOf(bid));
        return new JsonResponse(JsonResponse.SUCCESS, isok);
    }

    @RequestMapping(value = "/isCollect", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponse isCollect(@RequestParam Long uid, @RequestParam Long bid) {

        List<Video> videoList = demoVideoDao.isCollect(String.valueOf(uid),String.valueOf(bid));
        int isok = 0;
        if(videoList.size()>0){
            isok = 1;
        }
        return new JsonResponse(JsonResponse.SUCCESS, isok);
    }
}
